﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class MachinesEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sOp = Request.QueryString["Op"].ToString();
                int iID = 0;

                if (sOp == "Edit")
                {
                    iID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ID"].ToString()));
                    lblParent.Text = "Edit Machine ID " + iID.ToString();
                }
                else
                {
                    lblParent.Text = "Add Machine";
                }

                if (sOp == "Edit")
                {
                    string sAddress = Server.UrlDecode(Request.QueryString["Address"].ToString());
                    bool bIsVirtual = (Request.QueryString["IsVirtual"].ToString() == "Yes");
                    string sVMName = Server.UrlDecode(Request.QueryString["VMName"].ToString());
                    if (sVMName == "NULL")
                        sVMName = "";
                    string sVMMaxGuests = Server.UrlDecode(Request.QueryString["VMMaxGuests"].ToString());
                    string sDetails = Server.UrlDecode(Request.QueryString["Details"].ToString());
                    if (sDetails == "NULL")
                        sDetails = "";
                    bool bActive = (Request.QueryString["Active"].ToString() == "Active");
                    txtAddress.Text = sAddress;
                    checkIsVirtual.Checked = bIsVirtual;
                    txtVMName.Text = sVMName;
                    txtVMMaxGuests.Text = sVMMaxGuests;
                    txtDetails.Text = sDetails;
                    checkActive.Checked = bActive;
                }
                else
                {
                    cmdSave.Text = "Add";

                    txtAddress.Text = "";
                    checkIsVirtual.Checked = false;
                    txtVMName.Text = "";
                    txtVMMaxGuests.Text = "";
                    txtDetails.Text = "";
                    checkActive.Checked = true;
                }
            }

        }
        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sOp = Request.QueryString["Op"].ToString();
            string sUserID = Request.LogonUserIdentity.Name;
            if (sOp != "Edit")
            {
                if (txtAddress.Text.Trim().Length == 0)
                {
                    lblErrorMessage.Text = "ERROR: You must enter an address!";
                    return;
                }
            }

            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                int iVMMaxGuests = 0;
                if (txtVMMaxGuests.Text.Trim().Length > 0)
                    iVMMaxGuests = Convert.ToInt32(txtVMMaxGuests.Text.Trim());
                if (sOp == "Edit")
                {
                    int pID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ID"].ToString()));
                    objATBDB.UpdateTestMachine(pID, txtAddress.Text, checkIsVirtual.Checked, txtVMName.Text, iVMMaxGuests, txtDetails.Text, checkActive.Checked, sUserID);
                }
                else
                    objATBDB.AddTestMachine(txtAddress.Text, checkIsVirtual.Checked, txtVMName.Text, iVMMaxGuests, txtDetails.Text, checkActive.Checked, sUserID);
            }
            Response.Redirect("MachinesHome.aspx");
        }
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MachinesHome.aspx");
        }
    }
}